package com.humanminoritygame.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

import com.humanminoritygame.model.User;

public class Utils {
  private static MailSender mailSender;
  private static SimpleMailMessage templateMessage;

  @Autowired
  public Utils(MailSender mailSender, SimpleMailMessage templateConfirmationMessage){
    this.mailSender = mailSender;
    this.templateMessage = templateConfirmationMessage;
//    Utils.mailSender = mailSender;
//    Utils.templateMessage = templateConfirmationMessage;

  }

  public static String encrypt(String string) {
    byte[] defaultBytes = string.getBytes();
    String hexString = new String("");
    try{
      MessageDigest algorithm = MessageDigest.getInstance("SHA-256");
      algorithm.reset();
      algorithm.update(defaultBytes);
      byte messageDigest[] = algorithm.digest();
                
      for (int i=0;i<messageDigest.length;i++) {
        hexString= hexString + (Integer.toHexString(0xFF & messageDigest[i]));
      }
    }catch(NoSuchAlgorithmException nsae){
      throw new RuntimeException(nsae);
    }
    return hexString;
  }
  
  public static Boolean validatePattern(String string, Pattern pattern) {
    if(string == null || string.equals("")){
      return false;
    }
    if (!pattern.matcher(string).matches())
    {
        return false;
    }
    return true;
  }
 
  public static String writeXMLAttribute(String content, String attribute){
    return "<" + attribute + ">" + content + "</" + attribute +">\n";
  }

  public static void enviarMail(User user, String fullUrl) {
    String baseUrl = fullUrl.substring(0, fullUrl.indexOf("/servlets"));
    SimpleMailMessage msg =  new SimpleMailMessage(templateMessage);
    msg.setTo(user.getMail());
    msg.setFrom("no-reply@humanminoritygame.net");
    msg.setSubject("Human Minority Game - Email Confirmation");
    msg.setText("Hello " + user.getName() + "\n To confirm your subscription, please click the following link:\n "+ baseUrl + "/servlets/index/confirm?n=" + user.getConfirmedMail() + "&uid=" + user.getName());
    try{
      mailSender.send(msg);
    } catch(MailException ex) {
      // simply log it and go on...
     // log.fatal("Email Notification message could not sent", ex);
    //  System.out.println("send notification EXCEPTION!!!");
      throw new RuntimeException(ex);
    }
  }
  
  public static void enviarForgotPassMail(User user, String pass) {
    SimpleMailMessage msg =  new SimpleMailMessage(templateMessage);
    msg.setTo(user.getMail());
    msg.setFrom("no-reply@humanminoritygame.net");
    msg.setSubject("Human Minority Game - Password Recovery");
    msg.setText("Hello " + user.getName() + "\n Your new password is:\n" + pass + "\nUse it to log in to the Human Minority Game.");
    try{
      mailSender.send(msg);
    } catch(MailException ex) {
      // simply log it and go on...
     // log.fatal("Email Notification message could not sent", ex);
    //  System.out.println("send notification EXCEPTION!!!");
      throw new RuntimeException(ex);
    }
  }
}
